package com.wtwd.campus.send;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.utils.UnicodeUtil;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Spliterators;

/**
 * @author mjy
 * @date 2022/11/02
 * @description
 */
public class GetPhoneSender implements Sender {
    @Override
    public Command dealMessage(MqMessage mqMessage) {
        MqPhoneDatas phoneDatas = (MqPhoneDatas) mqMessage.getObject();
        String imei = phoneDatas.getImei();
        String commandNumber = phoneDatas.getCommandNumber();
        String type = phoneDatas.getType();
        String cmd = mqMessage.getType();
        List<PhoneData> phones = phoneDatas.getPhoneDatas();
        int groupNum = 0;
        String content = "";
        if (phones != null && phones.size() > 0) {
            groupNum = phones.size();
            for (PhoneData e : phones) {
                String name = e.getName();
                String phone = e.getPhone();
                String phoneType = e.getPhoneType();
                String key = e.getKey();
                if (StringUtils.isNotBlank(name)) {
                    name = UnicodeUtil.gbEncoding(name);
                }
                content += ",".concat(phoneType).concat(",").concat(key).concat(",").concat(phone).concat(",").concat(name);
            }
        }
        String header = "*".concat(Constants.MANUFACTURER_NAME).concat(",").concat(imei).concat(",").
                concat(cmd).concat(",").concat(commandNumber).concat(",").concat(type).concat(",").concat(String.valueOf(groupNum));

        String tail = "#";
        content = header.concat(content).concat(tail);
        Command command = new Command();
        command.setImei(imei);
        command.setContent(content);
        command.setCmd(cmd);
        command.setStatus(Constants.COMMAND_NO_SAVE);
        command.setCommandNumber(commandNumber);
        return command;
    }
}
